﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace Kiva7.RatingReminders
{
    public partial class ConfirmationBox : Grid
    {
        public Action YesAction;
        public Action NoAction;
        public Action RemindAction;
        private static bool AppBarVisibility { get; set; }
        
        private PhoneApplicationFrame RootFrame
        {
            get
            {
                return Application.Current.RootVisual as PhoneApplicationFrame;
            }
        }

        private PhoneApplicationPage CurrentPage
        {
            get
            {
                PhoneApplicationPage currentPage = null;
                if (RootFrame != null)
                {
                    currentPage = RootFrame.Content as PhoneApplicationPage;
                }

                return currentPage;
            }
        }

        public ConfirmationBox()
        {
            InitializeComponent();
        }


        public static void Show(string title, string message, Action yesAction, Action noAction, Action remindAction)
        {
            var confBox = new ConfirmationBox
            {
                TitleTextBlock = { Text = title },
                MessageTextBlock = { Text = message },
                YesAction = yesAction,
                NoAction = noAction,
                RemindAction = remindAction
            };
            confBox.Show();
        }

        private void Show()
        {
            HandleAppBar();

            var grid = System.Windows.Media.VisualTreeHelper.GetChild(CurrentPage, 0) as Grid;
            if (grid != null) grid.Children.Add(this);

            var transitionIn = new SwivelTransition {Mode = SwivelTransitionMode.BackwardIn};

            var transition = transitionIn.GetTransition(this);
            transition.Completed += (s, e) => transition.Stop();
            transition.Begin();

            if (CurrentPage.ApplicationBar != null)
                CurrentPage.ApplicationBar.IsVisible = false;
        }

        private void Hide()
        {
            var grid = System.Windows.Media.VisualTreeHelper.GetChild(CurrentPage, 0) as Grid;

            var transitionOut = new SwivelTransition {Mode = SwivelTransitionMode.BackwardOut};

            var transition = transitionOut.GetTransition(this);
            transition.Completed += (s, e) =>
                                        {
                                            transition.Stop();
                                            if (grid != null) grid.Children.Remove(this);
                                            if (CurrentPage.ApplicationBar != null)
                                                CurrentPage.ApplicationBar.IsVisible = true;
                                        };
            transition.Begin();
        }


        private void HandleAppBar()
        {
            if (CurrentPage.ApplicationBar == null)
                return;

            AppBarVisibility = CurrentPage.ApplicationBar.IsVisible;
            CurrentPage.ApplicationBar.IsVisible = false;
        }

        private void YesButtonClick(object sender, RoutedEventArgs e)
        {
            YesAction.Invoke();
            Hide();
        }

        private void NoButtonClick(object sender, RoutedEventArgs e)
        {
            NoAction.Invoke();
            Hide();
        }

        private void RemindButtonClick(object sender, RoutedEventArgs e)
        {
            RemindAction.Invoke();
            Hide();
        }
    }
}